package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbType;
import com.bwie.service.TbTypeService;
import com.bwie.mapper.TbTypeMapper;
import com.bwie.utils.R;
import com.bwie.utils.Result;
import com.bwie.utils.TokenUtils;
import com.github.tobato.fastdfs.FdfsClientConfig;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author lenovo
 * @description 针对表【tb_type(商品分类表)】的数据库操作Service实现
 * @createDate 2025-06-20 15:27:23
 */
@Service
public class TbTypeServiceImpl extends ServiceImpl<TbTypeMapper, TbType>
        implements TbTypeService {

    @Autowired
    TbTypeMapper tbTypeMapper;

    @Autowired
    FastFileStorageClient storageClient;
    @Autowired
    HttpServletRequest request;

    @Override
    public Result getTypeTreeList() {

        //查询所有分类
        List<TbType> list = tbTypeMapper.selectList(null);
        //构造树形结构
        List<Tree<String>> typeTree = TreeUtil.build(list, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid() + "");
            //拓展属性
            tree.putExtra("typeStatus", type.getTypeStatus());
            tree.putExtra("typeTop", type.getTypeTop());
            tree.putExtra("typeImage", type.getTypeImage());
            tree.putExtra("createTime", type.getCreatedTime());
        });

        return Result.ok().setData(typeTree);
    }

    @Override
    public Result addType(TbType tbType) {
        //名称检查 确保唯一
        TbType type = tbTypeMapper.selectOne(
                new QueryWrapper<TbType>().lambda().eq(TbType::getTypeName, tbType.getTypeName())
        );
        if (type != null) {
            return Result.error().setMsg("名称已存在");
        }
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error().setMsg("请先登录");
        }
        String userName = TokenUtils.tokenUtilsGetName(token);
        tbType.setCreatedBy(userName);
        tbType.setCreatedTime(new Date());
        //添加
        tbTypeMapper.insert(tbType);

        return Result.ok();
    }

    @Override
    public Result uploadTypeImage(MultipartFile file) {
        try {
            String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url = "http://192.168.80.192:8888/" + storePath.getFullPath();
            return Result.ok().setData(url);
        } catch (IOException e) {
//            throw new RuntimeException(e);
            e.printStackTrace();
            return Result.error().setMsg("文件上传失败");
        }

    }

    @Override
    public Result updateType(TbType tbType) {
        TbType type = tbTypeMapper.selectById(tbType.getTypeId());
        if (type == null) {
            return Result.error().setMsg("分类不存在");
        }
        //获取token
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error().setMsg("请先登录");
        }
        String userName = TokenUtils.tokenUtilsGetName(token);
        tbType.setUpdatedBy(userName);
        tbType.setUpdatedTime(new Date());
        tbTypeMapper.updateById(tbType);
        return Result.ok();
    }

    @Override
    public Result deleteType(Integer typeId) {
        TbType tbType = tbTypeMapper.selectById(typeId);
        if (tbType == null) {
            return Result.error().setMsg("分类不存在");
        }
        tbTypeMapper.deleteById(tbType);
        return Result.ok();
    }

    @Override
    public Result getSelectTypeTreeList() {
        //查询所有分类
        List<TbType> list = tbTypeMapper.selectList(null);
        //构造树形结构
        List<Tree<String>> typeTree = TreeUtil.build(list, "0", (type, tree) -> {
            tree.setId(type.getTypeId() + "");
            tree.setName(type.getTypeName());
            tree.setParentId(type.getTypePid() + "");
            //拓展属性
            tree.putExtra("label", type.getTypeName());
            tree.putExtra("value", type.getTypeId());
        });

        return Result.ok().setData(typeTree);
    }
}




